package com.drools.userguide._3_statefulSession;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertTrue;

public class Test {

    @org.junit.Test
    public void test() {
        KieServices kieServices = KieServices.Factory.get();
        KieContainer kContainer = kieServices.getKieClasspathContainer();
        KieSession kSession = kContainer.newKieSession();
        String[] names = new String[]{"kitchen", "bedroom", "office", "livingroom"};
        Map<String, Room> name2room = new HashMap<String, Room>();
        for (String name : names) {
            Room room = new Room(name);
            name2room.put(name, room);
            kSession.insert(room);
            Sprinkler sprinkler = new Sprinkler(room);
            kSession.insert(sprinkler);
        }

        kSession.fireAllRules();

        Fire kitchenFire = new Fire(name2room.get("kitchen"));
        Fire officeFire = new Fire(name2room.get("office"));

        FactHandle kitchenFireHandle = kSession.insert(kitchenFire);
        FactHandle officeFireHandle = kSession.insert(officeFire);

        kSession.fireAllRules();

        kSession.delete(kitchenFireHandle);
        kSession.delete(officeFireHandle);

        kSession.fireAllRules();
    }
}
